package com.tjp.eight.ui.fragment.comment;

import com.tjp.eight.AllRetrofit;
import com.tjp.eight.ResultModel;
import com.tjp.eight.base.BasePresenterImpl;
import com.tjp.eight.constant.UrlConstant;
import com.tjp.eight.net.NetCallBack;
import com.tjp.eight.net.SubscribeCallBack;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/2/24.
 */
public class CommentPresenter extends BasePresenterImpl<CommentView> {
    CommentService commentService = AllRetrofit.getRetrofit(UrlConstant.BaseUrl).create(CommentService.class);

    public CommentPresenter(CommentView commentView) {
        attachView(commentView);
    }

    public void loadData(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        addSubscription(commentService.loadCommentData(requestBody), new SubscribeCallBack<>(new NetCallBack<CommentResultModel>() {
            @Override
            public void onSuccess(CommentResultModel model) {
                view.loadCommentSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadCommentFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }
    public void sendComment(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        addSubscription(commentService.sendComment(requestBody), new SubscribeCallBack<>(new NetCallBack<SendCommentResultModel>() {
            @Override
            public void onSuccess(SendCommentResultModel model) {
                view.sendComment(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadCommentFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }
    public void zanComment(String zanJson) {
        RequestBody req = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), zanJson);
        addSubscription(commentService.zanComment(req), new SubscribeCallBack<>(new NetCallBack<ZanCommentResultModel>() {
            @Override
            public void onSuccess(ZanCommentResultModel model) {
                view.zanSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadCommentFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }
}
